package com.cndw.kungfu.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.cndw.kungfu.comm.AccessFlag;
import com.cndw.kungfu.comm.LocalCommand;
import com.cndw.kungfu.config.ConstantsCost;
import com.cndw.kungfu.config.ConstantsDayIntKey;
import com.cndw.kungfu.config.ConstantsFlag;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.config.ConstantsQueue;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.config.RMK;
import com.cndw.kungfu.domain.Friend;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Queue;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.MD5;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataStaticCenter;
import com.cndw.kungfu.model.battle.Team;
import com.cndw.kungfu.model.goods.domain.SubPops;
import com.cndw.kungfu.model.player.FormulaMarry;
import com.cndw.kungfu.model.sys.ChatMain;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.vo.EventNotice;
import com.cndw.kungfu.vo.PlayerChangeVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 结婚相关
 * 
 * @author fantadust
 * 
 */
@AccessFlag(ConstantsFlag.marry)
@Controller
public class MarryController {

	@LocalCommand
	public ResponseMessage test(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getDataInfo().getMlId() > 0) {
			Player target = Context.getPlayerServer().getByPlayerId(player.getDataInfo().getMlId());
			target.getDataInfo().setMlId(0);
			player.getDataInfo().setMlId(0);

			target.getDataInfo().commitSync();
			player.getDataInfo().commitSync();
		}

		return ResponseMessage.getSuccMessage(true);
	}

	public ResponseMessage flowerShow(Session session, long targetId) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int ffs = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.ffs);
		int freeMax = player.getLevel() < 30 ? 0 : player.getDataInfo().getMlId() > 0 ? 11 : 3;
		Player target = Context.getPlayerServer().getByPlayerId(targetId);
		if (target == null) {
			return null;
		}
		Friend friend1 = player.getDataInfo().getFriend(targetId);
		Friend friend2 = target.getDataInfo().getFriend(session.getUid());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ffs", ffs);
		map.put("freeMax", freeMax);
		map.put("costFlower", ConstantsModel.marryFlower);
		map.put("intimacy1", friend1 == null ? 0 : friend1.getIntimacy());
		map.put("intimacy2", friend2 == null ? 0 : friend2.getIntimacy());
		map.put("receiverVId", target.getVocationId());

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 赠送花
	 * 
	 * @param session
	 * @param targetId
	 * @param type
	 * @return
	 */
	public ResponseMessage flowerSend(Session session, long targetId, int type) {
		if (session.getUid() == targetId || type < 0 || type > 3) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Player target = Context.getPlayerServer().getByPlayerId(targetId);
		if (target == null) {
			return null;
		}
		int ffs = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.ffs);
		int freeMax = player.getLevel() < 30 ? 0 : player.getDataInfo().getMlId() > 0 ? 11 : 3;
		boolean free = type == 0 && freeMax > ffs;

		int[] arr = ConstantsModel.marryFlower[type];
		boolean costTools = !free && player.getPlayerBoxs().isHaveGoods(arr[3]);
		boolean costGold = !free && !costTools;
		if (costGold && player.getGoldAll() < arr[0]) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		if (costTools) {
			player.getPlayerBoxs().costGoods(arr[3], 1);
		} else if (costGold) {
			player.alterGoldAll(-arr[0]);
			LogServer.costEventLog(session.getUid(), arr[0], ConstantsCost.marryFlower);
		} else {// 免费
			ffs++;
			player.getDataInfo().getEveryDay().intDataSet(ConstantsDayIntKey.ffs, ffs);
		}

		player.commitSync();

		Friend friend1 = player.getDataInfo().getFriend(targetId);
		Friend friend2 = target.getDataInfo().getFriend(session.getUid());
		if (friend1 != null) {
			friend1.alterIntimacy(arr[1]);
			friend1.commitSync();
		}
		if (friend2 != null) {
			friend2.alterIntimacy(arr[2]);
			friend2.commitSync();
		} else {

		}
		DataStaticCenter.marryFlowerAdd(targetId, 1);
		EventNotice noticeSend = new EventNotice(ConstantsRadio.marry, ConstantsRadio.marryFlowerReceive);
		noticeSend.put("type", type);
		noticeSend.put("senderId", player.getPlayerId());
		noticeSend.put("senderName", player.getPlayerName());
		noticeSend.put("senderVId", player.getVocationId());
		noticeSend.broadcast(target.getPlayerId());

		if (type == 3) {// 单服广播
			EventNotice notice = new EventNotice(ConstantsRadio.marry, ConstantsRadio.marryFlower);
			notice.put("type", type);
			notice.put("sendId", player.getPlayerId());
			notice.put("sendName", player.getPlayerName());
			notice.put("sendVId", player.getVocationId());
			notice.put("receiverId", target.getPlayerId());
			notice.put("receiverName", target.getPlayerName());
			notice.put("receiverVId", target.getVocationId());
			notice.broadCastAllOnLine();
		}
		int intimacy1 = friend1 == null ? 0 : friend1.getIntimacy();
		int intimacy2 = friend2 == null ? 0 : friend2.getIntimacy();

		LogServer.recordLog(player, ConstantsLog.marry, 130001, type, arr[1], costTools, costGold ? arr[0] : 0, target.getPlayerName(), targetId, intimacy1,
				intimacy2);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put(RMK.gold, player.getGold());
		map.put(RMK.goldBing, player.getGoldBing());
		map.put(RMK.asset, player.getAsset());
		map.put("intimacy1", intimacy1);
		map.put("intimacy2", intimacy2);
		map.put("targetId", targetId);
		map.put("ffs", ffs);
		map.put("freeMax", freeMax);
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage flowerKiss(Session session, long targetId) {
		boolean ok = DataStaticCenter.marryFlowerAdd(session.getUid(), -1);
		if (!ok) {
			return null;
		}
		Player sendPlayer = Context.getPlayerServer().getByPlayerId(session.getUid());
		String message = LanguageLib.get("marry.flower.kiss");
		ChatMain.privateMessage(message, sendPlayer, targetId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("suc", true);
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage marryCheck(Session session, long targetId) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Player target = Context.getPlayerServer().getByPlayerId(targetId);
		Friend friend1 = player.getDataInfo().getFriend(targetId);
		Friend friend2 = target.getDataInfo().getFriend(session.getUid());
		int needIntimacy = 9999;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ok1", player.getLevel() >= ConstantsLevel.lv40Marry && target.getLevel() >= ConstantsLevel.lv40Marry);
		map.put("ok2", player.getDataInfo().getMlId() == 0 && target.getDataInfo().getMlId() == 0);
		map.put("ok3", player.getVocationId() != target.getVocationId() && target.getVocationId() == 3);
		map.put("ok4", friend1 != null && friend2 != null && friend1.getIntimacy() >= needIntimacy && friend2.getIntimacy() >= needIntimacy);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 举行婚礼
	 * 
	 * @param session
	 * @param type
	 * @return
	 */
	public ResponseMessage marryOpen(Session session, int type) {
		if (type < 0 || type > 3) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getDataInfo().getMlId() > 0) {
			return null;
		}
		Team team = player.getTeam();
		if (team == null || !team.isMaster(session.getUid()) || team.getMemberIds().size() != 2) {
			return null;
		}
		Player target = null;
		for (Player e : team.getAllPlayers()) {
			if (e.getPlayerId() != session.getUid()) {
				target = e;
				break;
			}
		}
		if (target == null) {
			return null;
		}
		if (target.getDataInfo().getMlId() > 0) {
			return null;
		}
		if (!(player.getLevel() >= ConstantsLevel.lv40Marry && target.getLevel() >= ConstantsLevel.lv40Marry)) {
			return null;
		}
		boolean okSex = player.getVocationId() != target.getVocationId() && (target.getVocationId() == 3);
		if (!okSex) {
			return null;
		}
		Friend friend1 = player.getDataInfo().getFriend(target.getPlayerId());
		Friend friend2 = target.getDataInfo().getFriend(player.getPlayerId());
		if (friend1 == null || friend2 == null) {
			return null;
		}
		int needIntimacy = 9999;
		if (friend1.getIntimacy() < needIntimacy) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("marry.open.intimacy", player.getPlayerName(), needIntimacy));
		} else if (friend2.getIntimacy() < needIntimacy) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("marry.open.intimacy", target.getPlayerName(), needIntimacy));
		}
		int[] cost = ConstantsModel.marryOpen[type];
		int costAsset = cost[0], costGold = cost[1];
		if (player.getAsset() < costAsset) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
		} else if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		EventNotice notice = new EventNotice(ConstantsRadio.marry, ConstantsRadio.marryApply);
		notice.put("type", type);
		notice.put("manId", player.getPlayerId());
		notice.put("manName", player.getPlayerName());
		notice.broadcast(target.getPlayerId());

		DataStaticCenter.marryApply(player.getPlayerId(), target.getPlayerId(), type);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("open", 1);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 婚礼应答
	 * 
	 * @param session
	 * @param ok
	 * @return
	 */
	public ResponseMessage marrryReply(Session session, boolean ok) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getDataInfo().getMlId() > 0) {
			return null;
		}
		Team team = player.getTeam();
		if (team == null || team.isMaster(session.getUid()) || team.getMemberIds().size() != 2) {
			return null;
		}
		long manId = team.getMaster().getUniqueId();
		int type = DataStaticCenter.marryCheck(manId, session.getUid());
		if (type < 0 || type > 3) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}
		if (!ok) {
			EventNotice notice = new EventNotice(ConstantsRadio.marry, ConstantsRadio.marryReply);
			notice.put("ok", false);
			notice.put("womanId", player.getPlayerId());
			notice.put("womanName", player.getPlayerName());
			notice.broadcast(manId);
			return ResponseMessage.getSuccMessage(false);
		}
		Player man = Context.getPlayerServer().getByPlayerId(manId);
		if (man.getDataInfo().getMlId() > 0 || player.getDataInfo().getMlId() > 0) {
			return null;
		}
		int[] cost = ConstantsModel.marryOpen[type];
		int costAsset = cost[0], costGold = cost[1];
		if (man.getAsset() < costAsset) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
		} else if (man.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		man.alterAsset(-costAsset);
		man.alterGoldAll(-costGold);
		if (costGold > 0) {
			LogServer.costEventLog(manId, costGold, ConstantsCost.marryOpen);
		}
		PlayerChangeVO changeVO = new PlayerChangeVO();
		changeVO.put(PlayerChangeVO.PLAYER_ASSET, man.getAsset());
		changeVO.put(PlayerChangeVO.PLAYER_GOLD, man.getGold());
		changeVO.put(PlayerChangeVO.PLAYER_GOLDBING, man.getGoldBing());
		changeVO.broadcast(man.getPlayerId());

		man.getDataInfo().setMlId(player.getPlayerId());// 设置配偶id
		player.getDataInfo().setMlId(man.getPlayerId());

		man.commitSync();
		man.getDataInfo().commitSync();
		player.getDataInfo().commitSync();

		String title = LanguageLib.get("marry.ring.title");
		String content = LanguageLib.get("marry.ring.content");
		List<SubPops> attachment = new ArrayList<SubPops>();
		attachment.add(new SubPops(cost[2], 1, 0, 0));
		Context.getMessageServer().sendSysMail(man.getPlayerId(), man.getPlayerName(), title, content, attachment);
		Context.getMessageServer().sendSysMail(player.getPlayerId(), player.getPlayerName(), title, content, attachment);

		EventNotice notice = new EventNotice(ConstantsRadio.marry, ConstantsRadio.marryReply);
		notice.put("ok", true);
		notice.put("manId", man.getPlayerId());
		notice.put("manName", man.getPlayerName());
		notice.put("womanId", player.getPlayerId());
		notice.put("womanName", player.getPlayerName());
		notice.broadcast(manId, session.getUid());

		notice.clear();
		notice = new EventNotice(ConstantsRadio.marry, ConstantsRadio.marryOpen);
		notice.put("type", type);
		notice.put("manId", man.getPlayerId());
		notice.put("manName", man.getPlayerName());
		notice.put("manVId", man.getVocationId());
		notice.put("womanId", player.getPlayerId());
		notice.put("womanName", player.getPlayerName());
		notice.put("womanVId", player.getVocationId());
		Set<Long> ids = DataStaticCenter.marryZFIds(manId);
		notice.broadcast(ids);

		LogServer.recordLog(player, ConstantsLog.marry, 130003, type, costGold, costAsset, cost[2], man.getPlayerId(), player.getPlayerId());
		LogServer.recordLog(man, ConstantsLog.marry, 130003, type, costGold, costAsset, cost[2], man.getPlayerId(), player.getPlayerId());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("open", 2);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 进行祝福
	 * 
	 * @param session
	 * @param type
	 * @return
	 */
	public ResponseMessage marryBless(Session session, long manId) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (!player.getPlayerBoxs().checkBlack()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.player.boxmin", 1));
		}
		boolean ok = DataStaticCenter.marryZFOk(manId, session.getUid());
		if (!ok) {
			return null;
		}
		Context.getGoodsServer().giveGoodsBing(session.getUid(), 103614, 1, 1);
		LogServer.recordLog(player, ConstantsLog.marry, 130004, manId);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("suc", true);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 送礼
	 * 
	 * @param session
	 * @param manId
	 * @param type
	 * @return
	 */
	public ResponseMessage marryGifts(Session session, long manId, long giftsId, int type) {
		if (type < 0 || type > 5) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int[] cost = ConstantsModel.marryGifts[type];
		if (player.getAsset() < cost[0]) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
		}
		int costGold = 0, gaveIds = 0, flowerNum = 0;
		if (cost[1] > 0) {
			costGold = ConstantsModel.marryFlower[cost[1]][0];
			gaveIds = ConstantsModel.marryFlower[cost[1]][3];
			flowerNum = ConstantsModel.marryFlower[cost[1]][1];
		}
		if (player.getGold() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		if (type < 3 && !DataStaticCenter.marryGiftOk(manId, session.getUid())) {
			return null;
		} else if (!DataStaticCenter.marryGiftOk(manId)) {
			return null;
		}
		Player target = Context.getPlayerServer().getByPlayerId(giftsId);
		if (giftsId != manId && manId != target.getDataInfo().getMlId()) {
			return null;// 非法接收人
		}
		Player spouse = Context.getPlayerServer().getByPlayerId(target.getDataInfo().getMlId());

		player.alterAsset(-cost[0]);
		player.alterGold(-costGold);
		player.commitSync();
		if (costGold > 0) {
			LogServer.costEventLog(manId, costGold, ConstantsCost.marryGifts);
		}

		String title = LanguageLib.get("marry.gifts.title", player.getPlayerName());
		String content = null;
		List<SubPops> attachment = new ArrayList<SubPops>();
		if (cost[0] > 0) {
			attachment.add(new SubPops(0, 0, cost[0], 0));
			content = LanguageLib.get("marry.gifts.content1", player.getPlayerName(), cost[0]);
		} else {
			attachment.add(new SubPops(gaveIds, 1, 0, 0));
			content = LanguageLib.get("marry.gifts.content2", player.getPlayerName(), flowerNum);
		}
		Context.getMessageServer().sendSysMail(giftsId, target.getPlayerName(), title, content, attachment);

		LogServer.recordLog(player, ConstantsLog.marry, 130005, type, costGold, cost[0], giftsId, target.getPlayerName());

		if (flowerNum == 999) {
			ChatMain.systemMessage(LanguageLib.get("marry.gifts.chat", player.getPlayerName(), target.getPlayerName(), spouse.getPlayerName(), 999));
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(RMK.asset, player.getAsset());
		map.put(RMK.gold, player.getGold());
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage loveTree(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		long mlId = player.getDataInfo().getMlId();
		int intimacy1 = 0;
		if (mlId > 0) {
			Friend friend = player.getDataInfo().getFriend(mlId);
			intimacy1 = friend != null ? friend.getIntimacy() : 0;
		}
		int xl = player.getDataInfo().intDataGet(ConstantsIntKey.xl);
		int costIntimacy = FormulaMarry.getCostIntimacy(xl + 1);

		List<Queue> queues = Context.getQueueServer().getQueueIds(session.getUid(), false);
		Queue queue = null;
		if (queues != null && !queues.isEmpty()) {
			Iterator<Queue> it = queues.iterator();
			while (it.hasNext()) {
				Queue queue2 = (Queue) it.next();
				if (queue2.getType() == ConstantsQueue.typeOnMarry) {
					queue = queue2;
					break;
				}
			}
		}
		Player spouse = Context.getPlayerServer().getByPlayerId(player.getDataInfo().getMlId());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("intimacy1", intimacy1);
		map.put("xl", xl);
		map.put("costIntimacy", costIntimacy);
		map.put("xlAdd", FormulaMarry.getAddv(xl));
		map.put("xlAddStep", FormulaMarry.xStep);
		// map.put("xlAddNext", FormulaMarry.getAddv(xl + 1));
		map.put("endTime", queue == null ? 0 : queue.getEndTime());
		map.put("fastAmt", ConstantsModel.accelerateSecondOfGold);
		if (spouse != null) {
			map.put("spouseId", spouse.getPlayerId());
			map.put("spouseName", spouse.getPlayerName());
			map.put("spouseVId", spouse.getVocationId());
		}
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 侠侣修炼
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage loveTreeDo(Session session) {
		List<Queue> queues = Context.getQueueServer().getQueueIds(session.getUid(), false);
		if (queues != null && !queues.isEmpty()) {
			Iterator<Queue> it = queues.iterator();
			while (it.hasNext()) {
				if (it.next().getType() == ConstantsQueue.typeOnMarry) {
					return null;// 有队列的时候不能执行
				}
			}
		}

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		long mlId = player.getDataInfo().getMlId();
		if (mlId <= 0) {
			return null;
		}
		Friend friend = player.getDataInfo().getFriend(mlId);
		if (friend == null) {
			return null;
		}
		int xl = player.getDataInfo().intDataGet(ConstantsIntKey.xl);
		if (xl / 9 >= player.getLevel()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("marry.tree.level"));
		}

		int costIntimacy = FormulaMarry.getCostIntimacy(xl + 1);
		if (friend.getIntimacy() <= costIntimacy || costIntimacy <= 0) {
			return null;
		}
		friend.alterIntimacy(-costIntimacy);
		friend.commitSync();

		int costSconds = FormulaMarry.getCostCD(xl);
		int beginTime = TimeProcesser.getUnixTime();
		int endTime = beginTime + costSconds;
		Queue queue = new Queue();
		queue.setPlayerId(session.getUid());
		queue.setType(ConstantsQueue.typeOnMarry);
		queue.setFuncType(ConstantsQueue.funcTypeMarryTreeStudy);
		queue.setTitle(player.getPlayerName());
		queue.setBeginTime(beginTime);
		queue.setEndTime(endTime);
		queue.setParam1(xl + 1);
		queue.setParam2(0);
		queue.setNextExecTime(endTime);
		Context.getQueueServer().insertQueue(queue);

		LogServer.recordLog(player, ConstantsLog.marry, 130006, costIntimacy, friend.getIntimacy(), xl);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("intimacy1", friend.getIntimacy());
		map.put("endTime", endTime);
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage loveTreeFast(Session session) {
		List<Queue> queues = Context.getQueueServer().getQueueIds(session.getUid(), false);
		Queue queue = null;
		if (queues != null && !queues.isEmpty()) {
			Iterator<Queue> it = queues.iterator();
			while (it.hasNext()) {
				Queue queue2 = (Queue) it.next();
				if (queue2.getType() == ConstantsQueue.typeOnMarry) {
					queue = queue2;
					break;
				}
			}
		}
		if (queue == null) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int laveTime = queue.getEndTime() - TimeProcesser.getUnixTime();
		if (laveTime + 3 <= 0) {// 还有3秒不能加速
			return null;
		}
		int costGold = NumberProcesser.ceil(laveTime, ConstantsModel.accelerateSecondOfGold);
		if (player.getKnightLevel() == 4) {
			costGold = 0;
		}
		if (costGold < 0) {
			return null;
		}

		if (costGold > 0) {
			if (player.getGoldAll() < costGold) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
			}
			player.alterGoldAll(-costGold);
			LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.marryXLFast);
		}

		queue.setEndTime(TimeProcesser.getUnixTime());
		queue.setParam2(1);
		Context.getQueueServer().immediately(queue);

		if (costGold > 0) {
			LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.gestFast);
		}
		int xl = player.getDataInfo().intDataGet(ConstantsIntKey.xl);
		int costIntimacy = FormulaMarry.getCostIntimacy(xl + 1);

		LogServer.recordLog(player, ConstantsLog.marry, 130006, costGold, xl);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("xl", xl);
		map.put("xlAdd", FormulaMarry.getAddv(xl));
		map.put("costIntimacy", costIntimacy);
		map.put("xlAddStep", FormulaMarry.xStep);
		// map.put("xlAddNext", FormulaMarry.getAddv(xl + 1));
		map.put(RMK.player, player.toPlayerVO());
		map.put(RMK.gold, player.getGold());
		map.put(RMK.goldBing, player.getGoldBing());
		map.put(RMK.costGold, costGold);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 申请离婚 1 协议 2离线申请,3强制
	 * 
	 * @param session
	 * @param type
	 * @return
	 */
	public ResponseMessage divorce(Session session, int type) {
		if (type < 1 || type > 3) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		long marryId = player.getDataInfo().getMlId();
		if (marryId == 0) {
			return null;
		}
		Player target = Context.getPlayerServer().getByPlayerId(marryId);
		if (target == null) {
			return null;
		}
		boolean ok = false;
		if (type == 1) {
			if (!target.isOnline()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("marry.divorce.online"));
			}
			String title = LanguageLib.get("marry.divorce.apply");
			int currentTime = TimeProcesser.getUnixTime();
			String sg = MD5.compile(session.getUid() + "|" + marryId + "|" + currentTime);
			Context.getTcpServer().broadcastConfirm(title, MarryController.class.getSimpleName(), "divorceConfirm", 3,
					new Object[] { sg, String.valueOf(currentTime) }, marryId);

		} else if (type == 2) {
			if (TimeProcesser.getUnixTime() - target.getLastLogin() <= 7 * 86400) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("marry.divorce.lost"));
			}
			ok = true;
		} else {
			if (player.getGoldAll() < ConstantsModel.marryDivorce) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
			}
			player.alterGoldAll(-ConstantsModel.marryDivorce);
			ok = true;
		}
		if (ok) {
			divorcePross(player, target, type);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("suc", ok);
		if (type == 3) {
			map.put(RMK.gold, player.getGold());
			map.put(RMK.goldBing, player.getGoldBing());
		}
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage divorceConfirm(Session session, String sign, String ts, boolean agree) {
		int t = Integer.valueOf(ts);
		if (t <= TimeProcesser.getUnixTime() - 300) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		long marryId = player.getDataInfo().getMlId();
		if (marryId == 0) {
			return null;
		}
		Player target = Context.getPlayerServer().getByPlayerId(marryId);
		if (target == null) {
			return null;
		}
		String sg = MD5.compile(marryId + "|" + session.getUid() + "|" + ts);
		if (!sg.equals(sign)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}
		if (!agree) {
			String title = LanguageLib.get("marry.divorce.applyNo");
			Context.getTcpServer().broadcastResMsg(ResponseMessage.ticpErrMsg(title), marryId);
			return null;
		} else {
			divorcePross(target, player, 1);
		}
		return null;
	}

	private void divorcePross(Player sendPlayer, Player target, int type) {
		sendPlayer.getDataInfo().setMlId(0);
		target.getDataInfo().setMlId(0);
		sendPlayer.getDataInfo().commitSync();
		target.getDataInfo().commitSync();
		Friend friend1 = sendPlayer.getDataInfo().getFriend(target.getPlayerId());
		Friend friend2 = target.getDataInfo().getFriend(sendPlayer.getPlayerId());
		friend1.setIntimacy(0);
		friend2.setIntimacy(0);
		friend1.commitSync();
		friend2.commitSync();
		EventNotice notice = new EventNotice(ConstantsRadio.marry, ConstantsRadio.marryDivorce);
		notice.put("ok", true);
		notice.broadcast(sendPlayer.getPlayerId(), target.getPlayerId());
		
		String title = LanguageLib.get("marry.divorce.title");
		Context.getMessageServer().sendSysMail(sendPlayer.getPlayerId(), sendPlayer.getPlayerName(), title, LanguageLib.get("marry.divorce.content",target.getPlayerName()));
		Context.getMessageServer().sendSysMail(target.getPlayerId(), target.getPlayerName(), title, LanguageLib.get("marry.divorce.content",sendPlayer.getPlayerName()));
		
		LogServer.recordLog(sendPlayer, ConstantsLog.marry, 130008, type, sendPlayer.getPlayerId());
		LogServer.recordLog(target, ConstantsLog.marry, 130008, type, sendPlayer.getPlayerId());
		
	}

}
